package com.xv.yan.dao.impl;

import com.xv.yan.beans.entity.AdminDo;
import com.xv.yan.dao.AdminDao;
import com.xv.yan.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//用来实现AdminDao
public class AdminDaoImpl implements AdminDao {
    /**
     * 根据用户名判断数据库密码
     * @param userName
     * @return
     */

    @Override
    public AdminDo validateLogin(String userName) {
        // 给我说说 为啥返回一个AdminDao  我不理解不知道
        // 改！
        try {
            Connection connection = DBUtil.getConnection();
            String sql = "select * from user where username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, userName);
            ResultSet resultSet = ps.executeQuery();
            AdminDo adminDo = new AdminDo();
            if(resultSet.next()){
                adminDo.setUserName(userName);
                adminDo.setPwd(resultSet.getString("password"));
                adminDo.setId(resultSet.getInt("id"));
            }
            return adminDo;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
